﻿package commands 
{
	import commands.classesinfo.*;
	
	import flash.utils.ByteArray;	
	/**
	 * ...
	 * @author wwx
	 * 50000001  桌子信息更新
	 */
	public class DeskInofUpdateCmd extends abCommand 
	{
		
		public function DeskInofUpdateCmd() //: abCommand(CommandType.CMD_TYPE_DESKINOFUPDATE) 
		{
			super(CommandType(CommandType.CMD_TYPE_DESKINOFUPDATE));
			cardsInfo = new Array();
		}
		
		public var msgSeq:int;//消息号
		public var errorNum:int;//错误号
		
		public var deskId:int;//桌子id
		public var playerId:int;//玩家id
		public var seatId:int;//座位id
		
		public var type:int;//类型type,	//type=1(更新游戏状态),
										//type=2(更新筹码)
										//type=3(玩家进入)
										//type=4(玩家退出)
										//type=5(玩家赢得筹码)
										//type=6(游戏结束，清理结果)
										//type=7(当前进行操作的玩家)
										//type=8(玩家选择的操作)
										
		//type=1时，
		public var status:int;//	//0,//未开始游戏
											//1,//准备游戏
											//2,//正在游戏
											//3,//全下
											//4,//弃牌
		
		//type=2时，									
		public var remainChip:int;//Int 桌子上剩余筹码
		public var totalInjectChip:int;//总下注筹码
		public var totalChip:int;//总筹码
		
		//type=3时，
		public var playerName:String;//玩家名字
		
		//type= 5时
		public var winChips:int;//赢得的筹码
		public var typeOfCard:int;//玩家牌类型
		/*
		   10：皇家同花顺
		   9：同花顺
		   8：四条
		   7：葫芦
		   6：同花
		   5：顺子
		   4：三条
		   3：两对
		   2：一对
		   1：高牌
		   0：未知牌型
		*/		   
		public var cardsInfo:Array;//CardInfo
		
		//type=7时
		public var waitTime:int;//等待时间,客户端从该值开始倒数到为0时停止
		
		//type=8时
		public var typeOfOpt:int;	//1：//下注
									//2：//加注
									//3：//跟
									//4：//过牌
									//5：//全下
									//6：//弃牌
		public var value:int;	//（1）下注 和 加注 时表示 “筹码”
								//（2）全下和弃牌时表示“状态”

		public function convertToCmd(bytes:ByteArray, offset:int, len:int):int
		{
			var ret:int = -1;
			msgSeq = (int) (bytes[offset + 0]) + ((int(bytes[offset + 1])) << 8) + ((int(bytes[offset + 2])) << 16) + ((int(bytes[offset + 3])) << 24);
			offset += 4;
			errorNum = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
			offset += 4;
			
			deskId = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
			offset += 4;
			playerId = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
			offset += 4;
			seatId = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
			offset += 4;
			type = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
			offset += 4;
			
			if (1 == type)
			{
				status = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
				offset += 4;
			}
			else if (2 == type)
			{
				remainChip = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
				offset += 4;	
				totalInjectChip = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
				offset += 4;
				totalChip = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
				offset += 4;
			}
			else if (3 == type)
			{
				var stringLen:int = 0;
				stringLen =  (int) (bytes[offset + 0]) + ((int(bytes[offset + 1])) << 8);
				offset += 2;
				bytes.position = offset;
				playerName = bytes.readMultiByte(stringLen, "gb2312");
				offset += stringLen;
			}
			else if (4 == type)
			{
				
			}
			else if (5 == type)
			{
				winChips = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
				offset += 4;
				typeOfCard = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
				offset += 4;
				
				ret = (int) (bytes[offset + 0]) + ((int(bytes[offset + 1])) << 8);
				offset += 2;
				
				//cardsInfo = new Array();
				var cardInfo:CardInfo = null;
				for (var i:int = 0; i < ret; i++)
				{
					cardInfo = new CardInfo();
					cardInfo.colorOfCard = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
					offset += 4;
					cardInfo.contentOfCard = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
					offset += 4;
					cardsInfo[i] = cardInfo;
				}
				
			}
			else if(7 == type)
			{
				waitTime = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
				offset += 4;
			}
			else if(8 ==type)
			{
				typeOfOpt = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
				offset += 4;
				value = (int) (bytes[offset + 0]) + ((int(bytes[offset+1])) << 8) + ((int(bytes[offset+2])) << 16) + ((int(bytes[offset+3])) << 24);
				offset += 4;
			}
			return 0;
		}	
		
	}
	
}